package com.i2bt.app.friend.model;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.friend.FriendDetailActivity;
import com.i2bt.bean.FriendBean;
import com.i2bt.bean.GetSharedBean;
import com.i2bt.bean.ShareBean;
import com.i2bt.util.FileSizeConvertUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class FriendDetailJsonHandle extends BaseJsonModel {
	
	public FriendDetailJsonHandle(BaseActivity activity) {
		super(activity);
	}

	@Override
	protected String logicToWrap() throws JSONException {
		
		switch (beanParam.getAct()) {
		
		case AndroidRestContract.DELETE_FRIEND :  {
			jsonStrParam = wrapDeleteFriend();
			break;
		}
		
		case AndroidRestContract.FRIEND_DETAIL_MYSHARED : {
			wrapGetMyShared();
			break;
		}
		
		case AndroidRestContract.FRIEND_DETAIL_FRIENDSHARED : {
			wrapGetFriendShared();
			break;
		}
		
		case AndroidRestContract.FRIEND_DETAIL_DELETE_MYSHARED : {
			wrapDeleteShared();
			break;
		}
		
		case AndroidRestContract.FRIEND_DETAIL_DELETE_FRIENDSHARED : {
			wrapDeleteShared();
			break;
		}
		
		case AndroidRestContract.FRIEND_DETAIL_DOWNLOAD_MYSHARED : {
			wrapDownloadShared();
			break;
		}
		
		case AndroidRestContract.FRIEND_DETAIL_DOWNLOAD_FRIENDSHARED : {
			wrapDownloadShared();
			break;
		}
		
		}
		
		return jsonStrParam;
	}

	/**
	 * 打包取消关注好友
	 * @return
	 * @throws JSONException
	 */
	private String wrapDeleteFriend() throws JSONException {
		
		wrapCommonJson();
		
		FriendBean friendBean = (FriendBean) beanParam;
		jsonParam.put(AndroidRestContract.FRIEND_ID, friendBean.getFriendId());
		
		return jsonParam.toString();
	}
	
	private String wrapGetMyShared() throws JSONException {
		
		wrapCommonJson();
		
		GetSharedBean getSharedBean = (GetSharedBean) beanParam;
		jsonParam.put(AndroidRestContract.FRIEND_NAME, 
				getSharedBean.getFriendName());
		
		return jsonParam.toString();
	}
	
	private String wrapGetFriendShared() throws JSONException {
		
		wrapCommonJson();
		
		GetSharedBean getSharedBean = (GetSharedBean) beanParam;
		jsonParam.put(AndroidRestContract.FRIEND_NAME, 
				getSharedBean.getFriendName());
		
		return jsonParam.toString();
		
	}
	
	private String wrapDeleteShared() throws JSONException {
		
		wrapCommonJson();
		ShareBean shareBean = (ShareBean) this.beanParam;
	    jsonParam.put(AndroidRestContract.SHARE_FOLDERS, shareBean.getPath());
	    jsonParam.put(AndroidRestContract.FRIEND_NAME, shareBean.getFriendName());
	    
		return jsonParam.toString();
	}
	
	private String wrapDownloadShared() throws JSONException {
		
		wrapCommonJson();
		ShareBean shareBean = (ShareBean) this.beanParam;
	    jsonParam.put(AndroidRestContract.SHARE_FOLDERS, shareBean.getPath());
	    jsonParam.put(AndroidRestContract.FRIEND_NAME, shareBean.getFriendName());
	    
		return jsonParam.toString();
	}
	
	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		
		if (isSuc) {
			jsonParam = new JSONObject(result);

			if (jsonParam.getBoolean(AndroidRestContract.SUCCEED)) {
				FriendDetailActivity friendDetailActivity = (FriendDetailActivity) activity;

				switch  (jsonParam.getInt(AndroidRestContract.ACTION)) {
				
				//取消关注
				case AndroidRestContract.DELETE_FRIEND : {
					friendDetailActivity.deleteFriendResponse();
					break;
				}
				
				case AndroidRestContract.FRIEND_DETAIL_MYSHARED : {
					friendDetailActivity.setMySharedData(unwrapGetShared());
					break;
				}
				
				case AndroidRestContract.FRIEND_DETAIL_FRIENDSHARED : {
					friendDetailActivity.setFriendSharedData(unwrapGetShared());
					break;
				}
				
				case AndroidRestContract.FRIEND_DETAIL_DELETE_MYSHARED : {
					friendDetailActivity.refreshMyShare();
					break;
				}
				
				case AndroidRestContract.FRIEND_DETAIL_DELETE_FRIENDSHARED : {
					friendDetailActivity.refreshFriendShare();
					break;
				}
				
				case AndroidRestContract.FRIEND_DETAIL_DOWNLOAD_MYSHARED : {
					break;
				}
				
				case AndroidRestContract.FRIEND_DETAIL_DOWNLOAD_FRIENDSHARED : {
					break;
				}

				}
			}
			else {
				activity.toastHandler(jsonParam.
						getString(AndroidRestContract.MESSAGE));
			}
		}
		else {
			activity.toastHandler(result);
		}
		
	}
	
	private List<GetSharedBean> unwrapGetShared() throws JSONException {
		JSONArray list = jsonParam
				.getJSONArray(AndroidRestContract.SHARE_INFO);
		List<GetSharedBean> shareData = new ArrayList<GetSharedBean>();
		if (list != null) {
			for (int i = 0; i < list.length(); i++) {
				JSONObject item = list.getJSONObject(i);
				GetSharedBean bean = new GetSharedBean();

				bean.setShareDate(item
						.getString(AndroidRestContract.SHARE_DATE));

				bean.setFileSize(item
						.getString(AndroidRestContract.FILE_SIZE));

				bean.setHomeBoxPath(item
						.getString(AndroidRestContract.SHARE_FOLDERS));

				bean.setFileName(item
						.getString(AndroidRestContract.SHARE_HOMEBOX_NAME));

				bean.setFile(this.isFile(bean.getFileSize()));
				bean.setHomebox(this.isHomeBox(
						bean.getHomeBoxPath(), bean.isFile()));
				if (!bean.isHomebox()) {
					bean.setFileName(this.getFileName(bean
							.getHomeBoxPath()));
				}
				bean.setFileSize(FileSizeConvertUtil
						.autoConvert(Integer.parseInt(bean
								.getFileSize())));

				shareData.add(bean);
			}
		}
		
		return shareData;
	}
	
	public boolean isFile(String size) {
		if (size.equals("0")) {
			return false;
		} else {
			return true;
		}
	}

	public boolean isHomeBox(String path, boolean isFile) {
		if (isFile) {
			return false;
		}
		if (path.indexOf('\\') == -1) {
			return true;
		}
		return false;
	}

	public String getFileName(String path) {
		String str = path;
		int k = 0;
		for (int i = 0; i < str.length(); i++) {
			k = str.indexOf('\\');
			if (k == -1) {
				break;
			}
			str = str.substring(k + 1);
		}
		return str;
	}
	
}
